<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/7/29
 * Time: 16:35
 */

namespace app\admin\controller;


use think\Request;

class Banner extends Base
{
    /**
     * 轮播列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $create_start_time = input('create_start_time');
            $create_end_time = input('create_end_time');
            $banner_status = input('banner_status');
            $banner_title = input('banner_title');
            $where['banner_status'] = ['between', [1, 2]];
            // 名称
            if(isset($banner_title) && $banner_title != ''){
                $where['banner_title'] = ['like', '%' . $banner_title . '%'];
            }
            // 状态
            if(isset($banner_status) && $banner_status != ''){
                $where['banner_status'] = $banner_status;
            }
            // 创建时间
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time != ''){
                $where['banner_time'] = ['between', [$create_start_time, $create_end_time]];
            }
            if(isset($create_start_time) && $create_start_time != '' && isset($create_end_time) && $create_end_time == ''){
                $where['banner_time'] = ['>', $create_start_time];
            }
            if(isset($create_start_time) && $create_start_time == '' && isset($create_end_time) && $create_end_time != ''){
                $where['banner_time'] = ['<', $create_end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = db('banner')->where($where)->count();// 获取总条数
            $lists = db('banner')
                ->field('banner_id, banner_title, banner_image, banner_status, banner_time, banner_sort')
                ->where($where)
                ->order('banner_sort desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function bannerAdd(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');
            $banner_content = input('banner_content');
            $banner_image = input('banner_image');
            if(empty($banner_image)){
                return json(['code' => 100, 'msg' => '请上传缩略图']);
            }
            if(empty($banner_content)){
                return json(['code' => 100, 'msg' => '请填写图文内容']);
            }
            
            $data = [
                'banner_title' => $banner_title,
                'banner_image' => $banner_image,
                'banner_status' => $banner_status,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_sort' =>$banner_sort,
                'banner_content' => $banner_content,
            ];
            $res = db('banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('banner/banneradd');
    }
    
    /**
     * 轮播图详情
     * @return mixed
     */
    public function bannerDetail(){
        $banner_id = input('banner_id');
        $detail = db('banner')
            ->field('banner_title, banner_image, banner_content, banner_sort, banner_status, banner_time')
            ->where('banner_id', $banner_id)
            ->find();
        $this->assign('banner', $detail);
        return $this->fetch('banner/bannerdetail');
    }
    
    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function bannerEdit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $banner_image = input('banner_image');
            $banner_status = input('banner_status');
            $banner_sort = input('banner_sort');
            $banner_content = input('banner_content');
            if(empty($banner_content)){
                return json(['code' => 100, 'msg' => '请填写图文内容']);
            }
            if($banner_status == 2){
                $count_down = db('banner')->where('banner_status', 1)
                    ->where('banner_id', 'NEQ', $id)
                    ->count();
                if($count_down == 0){
                    return json(['code' => 100, 'msg' => '不可下架全部轮播图']);
                }
            }
            
            $data = [
                'banner_title' => $banner_title,
                'banner_status' => $banner_status,
                'banner_sort' =>$banner_sort,
                'banner_content' => $banner_content,
                'banner_update_time' => date('Y-m-d H:i:s')
            ];
            
            if($banner_image){
                $data['banner_image'] = $banner_image;
            }
            
            $res = db('banner')->where('banner_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = db('banner')->where('banner_id', $banner_id)->find();
        $this->assign([
            'banner_id' => $banner_id,
            'banner' => $banner,
        ]);
        return $this->fetch('banner/banneredit');
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        
        $res = db('banner')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $count_down = db('banner')
            ->where('banner_status', 1)
            ->where('banner_id', 'NEQ', $banner_id)
            ->count();
        if($count_down == 0){
            return json(['code' => 100, 'msg' => '不可下架全部轮播图']);
        }
        $res = db('banner')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
    }
    
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = db('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function bannerDel(){
        $id = input('id');
        $banner = db('banner')->where('banner_id', $id)->field('banner_status')->find();
        if($banner['banner_status'] == 1){
            $count_down = db('banner')->where('banner_status', 1)
                ->where('banner_id', 'NEQ', $id)
                ->count();
            if($count_down == 0){
                return json(['code' => 100, 'msg' => '不可删除，删除后无上架中的轮播图']);
            }
        }
        
        $res = db('banner')->where('banner_id', $id)->setField('banner_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}